/*** 本文件由CSMIS2.0-ModelCreater(UTF-8)工具辅助生成，版本1.0 ***/

package com.ce.pub.message.action;

import java.lang.reflect.InvocationTargetException;
import java.sql.ResultSet;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;

import com.ce.framework.datamaintain.action.DataMaintainAction;
import com.ce.framework.datamaintain.logic.ISQLMapper;
import com.ce.framework.exception.AppException;
import com.ce.framework.util.BeanAccessUtil;
import com.ce.framework.util.DateUtil;
import com.ce.framework.util.GeneratorUUID;
import com.ce.pub.message.view.PubMessageView;
import com.ce.sys.login.model.User;
import com.ce.util.DbUtil;

/**
* 嫦娥软件有限公司 版权所有 2013
* 创建人：　andy
* 创建日期：2010-11-06
* 创建时间：09:07:50
* 功能描述：短消息 [PUB_T_MESSAGE]更改时用的Action
* ==============================================
* 修改历史
* 修改人		修改时间		修改原因
*
* ==============================================
*/
public class PubMessageAction extends DataMaintainAction 
{
	private static final long serialVersionUID = 1L;

	@Override
	protected void initVcId() throws AppException {
		// TODO Auto-generated method stub
   	 	try {
			BeanAccessUtil.copyProperty(po, "vcId", GeneratorUUID.generateUUID());
			BeanAccessUtil.copyProperty(po, "vcDelFlag", 0);
			BeanAccessUtil.copyProperty(po, "dtTime", new Date());
			BeanAccessUtil.copyProperty(po, "intSeeFlag", 0);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 开发人： andy
	 * 开发时间： 2013-11-6 上午11:09:35
	 * 功能描述： 用于和某人对话时得到这个人最新的发送信息
	 * 方法的参数和返回值
	 * @return
	 * @throws AppException
	 * String 
	 * ==================================
	 * 修改历史
	 * 修改人        修改时间      修改原因及内容
	 *
	 * ==================================
	 */
	public String getNewMsg()throws AppException{
		String sql = "select m.vc_content,p.vc_name as sendusername,m.dt_time from pub_t_message m ,sys_t_person p" +
				" where m.vc_send$user = p.vc_id and m.vc_send$user='"+this.getParameter("sendUserId")+"' and m.vc_receive$user='"+User.getUserId()+"' and m.int_see$flag = 0 order by m.dt_time asc";
		List<PubMessageView> msgList = (List<PubMessageView>)this.baselogic.executeSQLQuery(sql, new ISQLMapper(){
			public Object fromSQLQuery(ResultSet rs, int num) {
				// TODO Auto-generated method stub
				PubMessageView msg = new PubMessageView();
				try{
					msg.setSendTimeStr(DateUtil.format(rs.getTimestamp("dt_time"),"yyyy-MM-dd HH:mm:ss"));
					msg.setVcContent(rs.getString("vc_content"));
				    msg.setSendUserName(rs.getString("sendusername"));	
				}catch(Exception e){
					e.printStackTrace();
				}
				return msg;
			}
		});
		if(msgList!=null&&msgList.size()>0)
		   DbUtil.executeSqlUpdate("update pub_t_message m set m.int_see$flag=1 where m.vc_send$user='"+this.getParameter("sendUserId")+"' and m.vc_receive$user='"+User.getUserId()+"'");
		JSONArray retMsg = new JSONArray();
		retMsg.add(msgList);	
		return super.writeHTML(retMsg.toString().substring(1, retMsg.toString().length()-1));
	}
}

